package cisdi.mes.sm.rm.serviceImpl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cisdi.mes.sm.rm.dao.MeasureDocDao;
import cisdi.mes.sm.rm.dao.MeasureDocLogDao;
import cisdi.mes.sm.rm.dao.MatRequestDao;
import cisdi.mes.sm.rm.domain.SmRmMaterialRequest;
import cisdi.mes.sm.rm.domain.SmRmMeasureDoc;
import cisdi.mes.sm.rm.service.MatReturnMgr;
import cisdi.mes.sm.util.dao.BaseDao;
import cisdi.mes.sm.util.dao.IRvDao;

@Service
public class MatReturnMgrImpl implements MatReturnMgr {

	@Autowired
	private BaseDao baseDao;
	@Autowired
	private MeasureDocDao measureDocDao;
	@Autowired
	private MeasureDocLogDao measureDocLogDao;
	@Autowired
	protected IRvDao rvDao;
	@Autowired
	private MatRequestDao matReqDao;
	
	@Override
	public Map<String, Object> matReturn(String measureId,String deliveryType,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();

		String validString = validMatReturn(measureId);
		if (validString.equals("success")) {
			// 1.退货
			measureDocDao.matRtn(measureId);
			if (deliveryType.endsWith("配送")) {
				SmRmMaterialRequest matReq = matReqDao.getByReqId(measureDocDao.getById(measureId).getPurchaseOrder());
				matReq.setLogisticsStatus("RETURN");
				baseDao.update(matReq);
			}
			SmRmMeasureDoc measureDoc = measureDocDao.getById(measureId);
			measureDocLogDao.logMatRtn(measureDoc,  request);
			rvDao.sendMatRtnForM(measureId);
			map.put("success", true);
			map.put("message", "计量单退货成功");
		} else {
			map.put("success", false);
			map.put("message", validString);
		}

		return map;
	}

	private String validMatReturn(String measureId) {
		String validString = "";
		Boolean isRcv = false;
		SmRmMeasureDoc md = measureDocDao.getById(measureId);
		if (md != null && md.getStatus().equalsIgnoreCase("RECEIVE")) {
			isRcv = true;
		} else {
			validString += "*该计量单的进厂状态为" + md.getStatus() + "<br>";
		}
		if (isRcv) {
			validString = "success";
		}
		return validString;
	}

	public MatRequestDao getMatReqDao() {
		return matReqDao;
	}

	public void setMatReqDao(MatRequestDao matReqDao) {
		this.matReqDao = matReqDao;
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public MeasureDocDao getMeasureDocDao() {
		return measureDocDao;
	}

	public void setMeasureDocDao(MeasureDocDao measureDocDao) {
		this.measureDocDao = measureDocDao;
	}

	public MeasureDocLogDao getMeasureDocLogDao() {
		return measureDocLogDao;
	}

	public void setMeasureDocLogDao(MeasureDocLogDao measureDocLogDao) {
		this.measureDocLogDao = measureDocLogDao;
	}

	public IRvDao getRvDao() {
		return rvDao;
	}

	public void setRvDao(IRvDao rvDao) {
		this.rvDao = rvDao;
	}
}
